package com.livegoods.message;


import com.livegoods.commons.pojo.LivegoosMessage;
import com.livegoods.service.BuyactionService;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 此类是RabbitMQ的 监听类，当 MQ向队列中发送一个消息，此类马上回监听到消费队列，获取 消息数据，进行房屋预定功能
 */
@Component
public class BuyactionMessageListener {



    @Autowired
    private BuyactionService buyactionService;



    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(
                            name = "${livegoods.rabbitmq.buy.queue}",
                            autoDelete = "false"
                    ),
                    exchange = @Exchange(
                            name = "${livegoods.rabbitmq.buy.exchange}"
                    ),
                    key = {"${livegoods.rabbitmq.buy.routingKey}"}
            )
    })
    public Object onMessage(LivegoosMessage livegoosMessage){

        String houseId = livegoosMessage.getHouseId();
        String username = livegoosMessage.getUsername();

        boolean result = buyactionService.buyHouse(houseId,username);


        return result;
    }
}
